package co.edu.unal.bioingenium.kbmed.retrieval.knowledge.process;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import co.edu.unal.bioingenium.kbmed.retrieval.data.CollectionData;
import co.edu.unal.bioingenium.kbmed.retrieval.data.RetrievalData;
import co.edu.unal.bioingenium.kbmed.retrieval.text.process.QueryByWords;
import co.edu.unal.bioingenium.kbmed.retrieval.text.process.RankByWords;
import co.edu.unal.bioingenium.kbmed.retrieval.vo.QueryDocument;
import co.edu.unal.bioingenium.kbmed.retrieval.vo.RelevantDocument;
import java.util.List;
import java.util.Set;

/**
 *
 * @author alejandro
 */
public class RetrieveService {

    /**
     * 
     */
    private CollectionData collectionData;
    /**
     * 
     */
    private RankByWords rankByWords;
    /**
     * 
     */
    private RankByConcepts rankByConcepts;
    /**
     * 
     */
    private QueryByWords queryByWords;
    /**
     * 
     */
    private QueryByConcepts queryByConcepts;
    /**
     * 
     */
    private static RetrieveService retrieveServiceInstance;

    /**
     * 
     * @return
     */
    public static RetrieveService getInstance() {
        if (retrieveServiceInstance == null) {
            retrieveServiceInstance = new RetrieveService();
            retrieveServiceInstance.init(Configuration.COLLECTION_NAME);
        }
        return retrieveServiceInstance;
    }

    private RetrieveService() {
        rankByWords = new RankByWords();
        rankByConcepts = new RankByConcepts();
        queryByWords = new QueryByWords();
        queryByConcepts = new QueryByConcepts();
    }

    /**
     * 
     * @param collectionName
     */
    public void init(String collectionName) {
        collectionData = new CollectionData(collectionName);
    }

    /**
     * 
     * @param query
     * @return
     */
    public List<RelevantDocument> retrieveByWords(String query) {
        QueryDocument queryDocument = queryByWords.mapQueryToWords(query, collectionData.getWordCorpus());
        List<RelevantDocument> results = rankByWords.retrieveByWords(queryDocument, collectionData.getWordCorpus(), collectionData.getWordByDocument(), collectionData.getWordsFeatures());
        return results;
    }

    /**
     * 
     * @param query
     * @return
     */
    public void retrieveByConcepts(RetrievalData retrievalData, String query) {

        QueryDocument queryDocument = queryByConcepts.mapQueryToConcepts(query, collectionData.getConceptCorpus());
        retrievalData.setQueryDocument(queryDocument);
        rankByConcepts.retrieveByConcepts(retrievalData, collectionData.getConceptCorpus(), collectionData.getConceptByDocument(), collectionData.getConceptsFeatures());
    }

    /**
     * 
     * @param queryDocument 
     * @param givenDescriptorIds 
     * @return
     */
    public void retrieveByConcepts(RetrievalData retrievalData, Set<String> givenDescriptorIds) {
        queryByConcepts.mapQueryToConcepts(retrievalData.getQueryDocument(), collectionData.getConceptCorpus(), givenDescriptorIds);
        rankByConcepts.retrieveByConcepts(retrievalData, collectionData.getConceptCorpus(), collectionData.getConceptByDocument(), collectionData.getConceptsFeatures());
    }

    /**
     * 
     * @return
     */
    public CollectionData getCollectionData() {
        return collectionData;
    }
}
